/*
 *   Copyright 2006 Wei Lian <lianwei3@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "header.h"
#include "sound.h"
#include "socket.h"
#include "iosocket.h"
#include "fork.h"
#include "pidbyname.h"
#include "loadsave.h"
extern int sound_handler;
extern GtkAdjustment *adj_sound;
extern GtkWidget *button_player;
extern GtkWidget *entry_status;
extern GtkWidget *button_attach;
extern pid_t pid_player;
extern char new_url[];
gboolean tracing = FALSE;
pid_t traced_process;
int detach_flag;

gint
timerOut (void *)
{
  if (detach_flag == 2)
    return FALSE;

  ////sound volume
  int value = sound_read (sound_handler);
  if (gtk_adjustment_get_value (adj_sound) != (gdouble) value)
    {
      gtk_adjustment_set_value (adj_sound, (gdouble) value);
    }

  ////player
  if ((pid_player == -1) && GTK_TOGGLE_BUTTON (button_player)->active)
    {
      launch_player ();
    }

  return TRUE;
}

void
onadjustmentchanged (GtkAdjustment * adj)
{
  sound_write (sound_handler, (int) (adj->value));

}

void
onbuttonplayertoggled (GtkWidget * widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      kill_process (pid_player);
      launch_player ();
    }
  else
    kill_process (pid_player);
}

void
onbuttonattachtoggled (GtkWidget * widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)	////attach
    {
      ///////////////hook process
      traced_process = find_pid_by_name ("Win32Client.exe -r");
      if (traced_process == 0)
	{
	  gtk_entry_set_text (GTK_ENTRY (entry_status),
			      "You haven't launched feidian yet!");
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_attach),
					FALSE);
	}
      else
	{
	  if (!tracing)
	    {
	      if (ptrace
		  ((enum __ptrace_request) PTRACE_ATTACH, traced_process,
		   NULL, NULL) == -1)
		{
		  perror ("ptrace attach");
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
						(button_attach), FALSE);
		}
	      else
		{
		  detach_flag = 0;
		  tracing = TRUE;
		  gtk_entry_set_text (GTK_ENTRY (entry_status), "Attached");
		}
	    }
	  else
	    {
	      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_attach),
					    FALSE);
	    }
	}
    }
  else
    {
      detach_flag = 1;		////detach
    }
}

void
destroy (GtkWidget * widget, gpointer data)
{
  kill_process (pid_player);
  ///save config
  save_config ();

  if (tracing)
    detach_flag = 2;		////quit
  else
    gtk_main_quit ();
}
